package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.ReservationDTO;
import com.zzyl.entity.Reservation;
import com.zzyl.mapper.ReservationMapper;
import com.zzyl.service.ReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.ReservationCountByTimeVo;
import com.zzyl.vo.ReservationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class ReservationServiceImpl implements ReservationService {
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询取消数量
     * @return
     */
    @Override
    public Integer getCancelledReservationCount() {
        Integer i = reservationMapper.getCancelledReservationCount();
        return i;
    }

    /**
     * 查询时间段有多少人
     * @param time
     * @return
     */
    @Override
    public ReservationCountByTimeVo getCountByTime(String time) {

        Integer count = reservationMapper.getCountByTime(time);
        ReservationCountByTimeVo vo = new ReservationCountByTimeVo();
        vo.setCount(6 - count);
        vo.setTime(time);
        return vo;
    }

    /**
     * 新增预约
     * @param dto
     */
    @Override
    public void addReservation(ReservationDTO dto) {
        Reservation bean = BeanUtil.toBean(dto, Reservation.class);
        bean.setCreateTime(LocalDateTime.now());
        bean.setUpdateTime(LocalDateTime.now());
        bean.setStatus(0);
        String userId = UserThreadLocal.getSubject();
        long l = Long.parseLong(userId);
        bean.setCreateBy(l);
        bean.setUpdateBy(l);
        reservationMapper.addReservation(bean);
    }

    @Override
    public PageResponse getList(Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<ReservationVO> vo = reservationMapper.getList(status);
        return PageResponse.of(vo, ReservationVO.class);
    }

    @Override
    public void cancleReservation(Integer id) {
        reservationMapper.cancleReservation(id);
    }
}
